package org.openkanban.client.ui.presenter;

import org.openkanban.client.ui.board.KanbanEditView;
import org.openkanban.client.ui.board.WhiteBoardView;
import org.openkanban.client.util.RootPanel;
import org.openkanban.shared.dto.KanbanDto;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.smartgwt.client.util.JSON;
import com.smartgwt.client.util.SC;

/**
 * Callback that handles create kanban response
 */
public class CreateKanbanRequestCallback implements RequestCallback {

    private KanbanEditView kanbanEditView;

    public CreateKanbanRequestCallback(KanbanEditView kanbanEditView) {
	this.kanbanEditView = kanbanEditView;
    }

    @Override
    public void onResponseReceived(Request request, Response response) {
	KanbanDto kanbanDto = new KanbanDto(response.getText());
	if (response.getStatusCode() < 300 && kanbanDto.isCallSuccessfull()) {
	    WhiteBoardView whiteBoardView = RootPanel.main.getBoard().getWhiteBoardView();

	    RootPanel.main.getFooter().updateBoardHistoryData();
	    whiteBoardView.getActivityViewList().get(0).addKanban(kanbanDto);
	    kanbanEditView.clear();

	    whiteBoardView.publishNewKanban(JSON.decode("{ \"kanbanId\": \"" + kanbanDto.getKanbanId().toString() + "\" }"));
	} else {
	    SC.say("Fehler: CreateKanbanRequestCall fehlgeschlagen!");
	}
    }

    @Override
    public void onError(Request request, Throwable exception) {

    }

}
